package xyz.xtt.equity.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.nacos.shaded.com.google.common.primitives.Longs;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.hutool.core.util.IdUtil;
import xyz.xtt.equity.entity.AssetBenefitPackage;
import xyz.xtt.equity.mapper.AssetBenefitPackageMapper;
import xyz.xtt.equity.service.IAssetBenefitPackageService;

/**
 * <p>
 * 资产编号流转记录表 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-03-17
 */
@Service
public class AssetBenefitPackageServiceImpl extends ServiceImpl<AssetBenefitPackageMapper, AssetBenefitPackage> implements IAssetBenefitPackageService {

	@Transactional(rollbackFor = Exception.class)
	@Override
	public Boolean installByPackageIds(String packageIds, Long assetId) {
		String[] split = packageIds.split(",");
		List<AssetBenefitPackage> assetBenefitPackageList = new ArrayList<>();
		for (String s : split) {
			Long aLong = Longs.tryParse(s);
			if (Objects.nonNull(aLong)) {
				AssetBenefitPackage assetBenefitPackage = AssetBenefitPackage
						.builder()
						.id(IdUtil.getSnowflakeNextId())
						.assetId(assetId)
						.packageId(aLong)
						.build();
				assetBenefitPackageList.add(assetBenefitPackage);
			}
		}
		return saveBatch(assetBenefitPackageList);
	}
}
